package com.kfm.am;

import java.util.Objects;

public class Student implements Comparable {

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;
        Student student = (Student) object;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
//        return age * 31 + name.hashCode() * 31;
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Object o) {
        if (o == null ) {
            throw new NullPointerException();
        }
        if (this.equals(o)) {
            return 0;
        }
        if (o instanceof Student student) {
            return age - student.getAge() > 0 ? 1 : -1;
        }
        throw new ClassCastException();
    }
}
